package com.xjscrm.console.chat.mq.impl;

import cn.hutool.core.util.IdUtil;
import com.uzai.mobile.collect.api.dto.msg.MicaFriendTalkMsg;
import com.xjscrm.common.entity.WwCustomer;
import com.xjscrm.common.entity.WwDevice;
import com.xjscrm.common.entity.WwGroup;
import com.xjscrm.console.chat.enums.ChatTalkRecordSendStatus;
import com.xjscrm.console.entity.ChatTalkRecord;
import com.xjscrm.console.entity.ChatTalkSession;
import com.xjscrm.console.service.customer.chat.ChatTalkSessionService;
import com.xjscrm.console.service.customer.group.WwGroupService;
import com.xjscrm.console.service.customer.wwCustomer.WwCustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Objects;

/**
 * @author mingyang.chen create in 2024/3/20 15:12
 */
@Slf4j
public class TalkMsgHandler {

    @Autowired
    private ChatTalkSessionService chatTalkSessionService;
    @Autowired
    private WwCustomerService wwCustomerService;
    @Autowired
    private WwGroupService wwGroupService;

    protected ChatTalkSession build(WwDevice device, MicaFriendTalkMsg micaFriendTalkMsg) {
        ChatTalkSession newTalkSession = new ChatTalkSession();

        newTalkSession.setId(IdUtil.getSnowflakeNextId());
        newTalkSession.setMerId(device.getMerId());
        newTalkSession.setDeviceId(device.getDeviceId());
        newTalkSession.setDeviceType(device.getDeviceType());
        newTalkSession.setDeviceUniqueId(device.getId());
        newTalkSession.setConversationId(micaFriendTalkMsg.getConversationId());
        newTalkSession.setConversationName(micaFriendTalkMsg.getConversationName());
        newTalkSession.setType(micaFriendTalkMsg.getGroup() ? 111 : 110);
        newTalkSession.setLastRecvTime(micaFriendTalkMsg.getSendTime().intValue());

        newTalkSession.setOpenShield(0);

        newTalkSession.setCreateTime((int) (System.currentTimeMillis() / 1000L));

        return newTalkSession;
    }

    protected ChatTalkRecord build(MicaFriendTalkMsg micaFriendTalkMsg, ChatTalkSession session) {
        ChatTalkRecord chatTalkRecord = new ChatTalkRecord();

        chatTalkRecord.setMerId(session.getMerId());
        chatTalkRecord.setTalkSessionId(session.getId());
        chatTalkRecord.setDeviceUniqueId(session.getDeviceUniqueId());
        chatTalkRecord.setConversationId(session.getConversationId());
        chatTalkRecord.setSendWxId(micaFriendTalkMsg.getSendWxId());
        chatTalkRecord.setSendWxName(micaFriendTalkMsg.getSendNickName());
        chatTalkRecord.setContentType(micaFriendTalkMsg.getMsgType());

        chatTalkRecord.setContent(micaFriendTalkMsg.getJsonContent());
        chatTalkRecord.setSendTime(micaFriendTalkMsg.getSendTime().intValue());
        chatTalkRecord.setWxSendTime(micaFriendTalkMsg.getSendTime().intValue());
        chatTalkRecord.setSendStatus(ChatTalkRecordSendStatus.send_success.getCode());
        chatTalkRecord.setRemoteMsgId(micaFriendTalkMsg.getRemoteMsgId());
        chatTalkRecord.setMsgId(micaFriendTalkMsg.getMsgId());
        chatTalkRecord.setCreateTime((int) (System.currentTimeMillis() / 1000L));

        return chatTalkRecord;
    }

    public ChatTalkSession addSessionIfAbsentWithUpdate(Long merId, Long talkRecordId, WwDevice wwDevice, MicaFriendTalkMsg micaFriendTalkMsg) {

        String conversationId = micaFriendTalkMsg.getConversationId();
        String deviceId = wwDevice.getDeviceId();

        String conversationName;
        String conversationAvatar;
        if (!micaFriendTalkMsg.getGroup()) {
            WwCustomer customer = wwCustomerService.getByWxId(merId, wwDevice.getId(), conversationId);
            if (Objects.isNull(customer)) {
                log.error("FriendTalk with customer is not found, next create new customer");
                return null;
//                customer = new WwCustomer();
//                customer.setId(IdUtil.getSnowflakeNextId());
//                customer.setMerId(merId);
//                customer.setDeviceUniqueId(wwDevice.getId());
//                customer.setWxid(micaFriendTalkMsg.getConversationId());
//                customer.setName(micaFriendTalkMsg.getConversationName());
//                customer.setPushTime((int) (System.currentTimeMillis() / 1000L));
//                customer.setDescription("客服系统主动创建");
//                customer.setCreateTime((int) (System.currentTimeMillis() / 1000L));
//                customer.setUpdateTime((int) (System.currentTimeMillis() / 1000L));
//                wwCustomerService.insert(customer);
            } else {
                conversationAvatar = customer.getHeadImgUrl();
                conversationName = customer.getName();
            }
        }
        //
        else {
            WwGroup byGroupId = wwGroupService.getByGroupIdAndDeviceId(merId, deviceId, conversationId);
            if (Objects.isNull(byGroupId)) {
                log.error("FriendTalk with group is not found, stop opt");
                return null;
            }
            conversationName = byGroupId.getGroupName();
            conversationAvatar = "";
        }

        log.info("name: {}, avatar: {}", conversationName, conversationAvatar);

        ChatTalkSession talkSession = chatTalkSessionService.getByConvIdAndDeviceUniqueId(merId, wwDevice.getId(), conversationId);
        //
        if (Objects.isNull(talkSession)) {
            talkSession = this.build(wwDevice, micaFriendTalkMsg);

            talkSession.setConversationName(conversationName);
            talkSession.setConversationAvatar(conversationAvatar);
            talkSession.setLastRecvId(talkRecordId);
            talkSession.setLastRecvTime(micaFriendTalkMsg.getSendTime().intValue());

            chatTalkSessionService.add(talkSession);
        } else {
            ChatTalkSession update = new ChatTalkSession();

            update.setId(talkSession.getId());
            update.setMerId(merId);
            update.setConversationName(conversationName);
            update.setConversationAvatar(conversationAvatar);
            update.setLastRecvId(talkRecordId);
            update.setLastRecvTime(micaFriendTalkMsg.getSendTime().intValue());
            update.setUpdateTime((int) (System.currentTimeMillis() / 1000L));

            chatTalkSessionService.update(update);
        }

        return talkSession;
    }
}
